/*
 * *
 *  *    Copyright 2009-2020 the original author or authors.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *       http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package com.young.core.dao.impl;

import com.young.common.query.Pagination;
import com.young.common.query.PaginationBuilder;
import com.young.common.query.Query;
import com.young.core.dao.BaseDao;
import com.young.core.dao.ProjectDao;
import com.young.core.domain.ProjectDO;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuguanqing
 * Date: 2020/11/10 12:36 下午
 **/
@Component
public class ProjectDaoImpl extends BaseDao implements ProjectDao {

    @Override
    public void insert(ProjectDO project) {
        this.sqlSession.insert("ProjectMapper.insert",project);
    }


    @Override
    public int update(ProjectDO project) {
        return this.sqlSession.update("ProjectMapper.update",project);
    }


    @Override
    public void audit(int id, boolean auditable) {
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("id",id);
        parameters.put("auditable",auditable ? 1 : 0);
        this.sqlSession.update("ProjectMapper.audit",parameters);
    }

    @Override
    public List<ProjectDO> search(String pattern) {
        return this.sqlSession.selectList("ProjectMapper.search",pattern);
    }

    @Override
    public ProjectDO get(String name) {
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("name",name);
        return this.sqlSession.selectOne("ProjectMapper.get",parameters);
    }

    @Override
    public ProjectDO get(int id) {
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("id",id);
        return this.sqlSession.selectOne("ProjectMapper.get",parameters);
    }

    @Override
    public Pagination<ProjectDO> query(Query query) {
        Map<String, Object> parameters = query.buildParameters();
        int amount = amount(parameters);
        PaginationBuilder<ProjectDO> builder = PaginationBuilder.query(query);
        builder.amount(amount);
        if (amount > 0) {
            List<ProjectDO> list = this.sqlSession.selectList("ProjectMapper.query", parameters);
            builder.result(list);
        } else {
            builder.result(Collections.EMPTY_LIST);
        }
        return builder.build();
    }

    private int amount(Map<String, Object> parameters) {
        return this.sqlSession.selectOne("ProjectMapper.getAmount", parameters);
    }

    @Override
    public int delete(int id) {
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("id",id);
        return this.sqlSession.delete("ProjectMapper.delete",parameters);
    }

    @Override
    public int delete(String name) {
        Map<String,Object> parameters = new HashMap<>();
        parameters.put("name",name);
        return this.sqlSession.delete("ProjectMapper.delete",parameters);
    }
}
